package com.hemaapp.yjnh.adapter;

import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;

import xtom.frame.image.load.XtomImageTask.Size;
import xtom.frame.view.XtomListView;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.hemaapp.hm_FrameWork.view.RoundedImageView;
import com.hemaapp.yjnh.BaseAdapter;
import com.hemaapp.yjnh.R;
import com.hemaapp.yjnh.bean.Family;
import com.hemaapp.yjnh.nettask.ImageAvatarTask;
import com.hemaapp.yjnh.utils.ImageUtils;

/**家族的成员排名，根据粮票的数量
 * @author GuiyouZhang
 * Date：2015年10月12日 下午1:01:17
 * Email:guiyouzhang@163.com
 */
public class RankingAdapter extends BaseAdapter {

	private ArrayList<Family> families;
	private Context context;
	private XtomListView listview;
	
	public RankingAdapter(Context mContext, ArrayList<Family> families, 
			XtomListView listview) {
		super(mContext);
		this.context = mContext;
		this.families = families;
		this.listview = listview;
	}

	@Override
	public int getCount() {
		int size = families == null ? 0 : families.size();
		return size == 0 ? 1 : families.size();
	}

	@Override
	public boolean isEmpty() {
		int size = families == null ? 0 : families.size();
		return size == 0;
	}

	@Override
	public Object getItem(int position) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public long getItemId(int position) {
		// TODO Auto-generated method stub
		return 0;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		if (isEmpty())
			return getEmptyView(parent);
		ViewHolder holder;
		if (convertView == null) {
			holder = new ViewHolder();
			convertView = LayoutInflater.from(mContext).inflate(
					R.layout.listitem_rank, null);
			findView(convertView, holder);
			convertView.setTag(holder);
		}else {
			holder = (ViewHolder) convertView.getTag();
		}
		setView(convertView, holder, position);
		return convertView;
	}
	
	private class ViewHolder {
		RoundedImageView avatarImg;
		TextView tv_name;
		TextView tv_coupon;
		LinearLayout ll_rank_text;
		TextView tv_rank;
		ImageView iv_rank;
		View listitem_divider;
	}
	
	private void findView(View convertView, ViewHolder holder) {
		holder.avatarImg = (RoundedImageView) convertView.findViewById(R.id.avatar);
		holder.tv_name = (TextView) convertView.findViewById(R.id.tv_name);
		holder.tv_coupon = (TextView) convertView.findViewById(R.id.tv_coupons_count);
		holder.ll_rank_text = (LinearLayout) convertView.findViewById(R.id.ll_rank_text);
		holder.tv_rank = (TextView) convertView.findViewById(R.id.tv_rank);
		holder.iv_rank = (ImageView) convertView.findViewById(R.id.iv_rank);
		holder.listitem_divider = convertView.findViewById(R.id.listitem_divider);
	}
	
	private void setView(View convertView, ViewHolder holder, int position) {
		Family family = families.get(position);
		ImageUtils.loadAvatar(mContext, family.getAvatar(), holder.avatarImg);
		holder.tv_name.setText(family.getnickname());
		holder.tv_coupon.setText(family.getscore());
		
		//设置排名
		if (position == 0) {
			family.setRank(1);
		}else {
			Family beforFamily = families.get(position-1);
			Double a = Double.parseDouble(family.getscore());
			Double b = Double.parseDouble(beforFamily.getscore());
			if (b-a==0) {
				family.setRank(beforFamily.getRank());
			}else if (b-a>0) {
//				family.setRank(beforFamily.getRank()+1);
				if (position == 1) {
					family.setRank(beforFamily.getRank()+1);
				}else {
					for (int i = 2; i < position+1; i++) {
						Family tempt = families.get(position-i);
						Double c = Double.parseDouble(tempt.getscore());
						if (c-b>0) {
							family.setRank(beforFamily.getRank()+i-1);
							break;
						}
					}
				}
			}else {
				//错误数据，排在最后
				family.setRank(families.size());
			}
		}
		setRankInfo(family.getRank(), holder);
	}
	
	/**根据排名来设置显示的排名
	 * @param rank
	 * @param holder
	 */
	private void setRankInfo(int rank, ViewHolder holder) {
		if (rank < 4) {
			holder.ll_rank_text.setVisibility(View.GONE);
			holder.iv_rank.setVisibility(View.VISIBLE);
			if (rank == 1) {
				holder.iv_rank.setImageResource(R.drawable.rank_1);
			}else if (rank == 2) {
				holder.iv_rank.setImageResource(R.drawable.rank_2);
			}else if (rank == 3) {
				holder.iv_rank.setImageResource(R.drawable.rank_3);
			}
		}else {
			holder.iv_rank.setVisibility(View.GONE);
			holder.ll_rank_text.setVisibility(View.VISIBLE);
			holder.tv_rank.setText(rank+"");
		}
	}
}
